package com.lg.happyfarmer;

import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.lg.happyfarmer.activity.HomePageActivity;
import com.lg.happyfarmer.base.BaseActivity;
import com.lg.happyfarmer.base.BaseFragmentActivity;
import com.lg.happyfarmer.fragment.EasySelectFragment;


public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawer;
    private RelativeLayout mRlMenu;
    private RelativeLayout right_menu;
    private FrameLayout  mLlContentMain;
    private NestedScrollView mNestedScrollView;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private AppBarLayout mAppBarLayout;
    private Toolbar mToolbar;
    private String[] mTitles = {"近七日销量榜", "近30日销量榜", "全粮票销量榜"};
    private View fl_search_top;
    private TabLayout tabLayout;
    private TextView mTvTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_drawerlayout);
        super.onCreate(savedInstanceState);
        initFirst();
        setListener();
    }
    
    private void  initFirst(){
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int windowsWight = metric.widthPixels;
        int windowsHeight = metric.heightPixels;
        mRlMenu = (RelativeLayout) findViewById(R.id.rl_menu);
        right_menu= (RelativeLayout) findViewById(R.id.right_menu);
        ViewGroup.LayoutParams leftParams = mRlMenu.getLayoutParams();
        leftParams.height = windowsHeight;
        leftParams.width = windowsWight;
        mRlMenu.setLayoutParams(leftParams);
        ViewGroup.LayoutParams rightParams = right_menu.getLayoutParams();
        rightParams.height = windowsHeight;
        rightParams.width = windowsWight;
        right_menu.setLayoutParams(rightParams);
        mLlContentMain = (FrameLayout) findViewById(R.id.ll_content_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        fl_search_top = mToolbar.findViewById(R.id.fl_search_top);
        mTvTitle = (TextView) mToolbar.findViewById(R.id.tv_title);
        setSupportActionBar(mToolbar);
        mAppBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mNestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
        //设置 NestedScrollView 的内容是否拉伸填充整个视图，
        //这个设置是必须的，否者我们在里面设置的ViewPager会不可见
        mNestedScrollView.setFillViewport(true);
        //添加tab
        tabLayout.addTab(tabLayout.newTab().setText("易选"));
        tabLayout.addTab(tabLayout.newTab().setText("易店"));
        tabLayout.addTab(tabLayout.newTab().setText("易行"));
        mTabLayout.setupWithViewPager(mViewPager);
         MyAdapter adapter = new  MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int scrollRangle = appBarLayout.getTotalScrollRange();
                //初始verticalOffset为0，不能参与计算。
                if (verticalOffset == 0) {
                    fl_search_top.setAlpha(0.0f);
                } else {
                    //保留一位小数
                    float alpha = Math.abs(Math.round(1.0f * verticalOffset / scrollRangle) * 10) / 10;
                    fl_search_top.setAlpha(alpha);
                }
            }
        });
    }
    private class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new EasySelectFragment();
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    protected void setListener() {

        mDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //设置主布局随菜单滑动而滑动
                if(drawerView.getId()==mRlMenu.getId()){

                    int drawerViewWidth = drawerView.getWidth();
                    mLlContentMain.setTranslationX(drawerViewWidth * slideOffset);
//                mLlContentMain.setTranslationX(0);
                    //设置控件最先出现的位置
                    double padingLeft = drawerViewWidth * (1 - 0.618) * (1 - slideOffset);
                    mRlMenu.setPadding((int) padingLeft, 0, 0, 0);

                }else if(drawerView.getId()==right_menu.getId()){
                    int drawerViewWidth = drawerView.getWidth();
                    mLlContentMain.setTranslationX(-drawerViewWidth * slideOffset);
//                mLlContentMain.setTranslationX(0);
                    //设置控件最先出现的位置
                    double padingLeft = drawerViewWidth * (1 - 0.618) * (1 - slideOffset);
                    mRlMenu.setPadding(0, 0, (int) padingLeft, 0);
                }
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }
    @Override
    public void onBackPressed() {
        if (mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
